package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

//套餐
@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    //分页查询套餐
    @Override
    public Page<Setmeal> findByPage(Integer pageNum, Integer pageSize, String name) {
        //1.根据name 模糊分页查询套餐
        //select * from setmeal where name like '%儿童%' limit 0,10
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(name),Setmeal::getName,name);
        //构建分页
        Page<Setmeal> page = new Page<>(pageNum, pageSize);
        page=setmealMapper.selectPage(page,qw);
        //2.根据setmeal的category_id 查询category
        for (Setmeal setmeal : page.getRecords()) {
            //查询分类名称category 主键id
            Category category = categoryMapper.selectById(setmeal.getCategoryId());
            setmeal.setCategoryName(category.getName());
            //3.根据setmeal的id查询setmeal_dish列表
            //select * from setmeal_dish where setmeal_id =1; 非主键
            LambdaQueryWrapper<SetmealDish> sDQw = new LambdaQueryWrapper<>();
            sDQw.eq(SetmealDish::getSetmealId,setmeal.getId());
            List<SetmealDish> SdList = setmealDishMapper.selectList(sDQw);
            setmeal.setSetmealDishes(SdList);
            //4.组装返回
        }
        return page;
    }
    //新增套餐
    @Override
    public void save(Setmeal setmeal) {
        //1.保存套餐基本信息
        //insert into setmeal() values();
        setmealMapper.insert(setmeal);
        //2.取出前端提供的菜品几个遍历添加到数据库中
        List<SetmealDish> DishList = setmeal.getSetmealDishes();
        for (SetmealDish setmealDish : DishList) {
            //2-1关联菜品id
            setmealDish.setSetmealId(setmeal.getId());
            //2-2保存数据库
           // inser into setmeal_dish() values
            setmealDishMapper.insert(setmealDish);
        }


    }
    //修改套餐
    //回显
    @Override
    public Setmeal findById(Long setId) {
        //1.查询套餐基本信息
        //select * from setmeal where id =?;
        Setmeal setmeal = setmealMapper.selectById(setId);
        //2.构建菜品查询条件
        //select * from setmeal_dish where setmeal_id=1;
        LambdaQueryWrapper<SetmealDish> sDQw = new LambdaQueryWrapper<>();
        sDQw.eq(SetmealDish::getSetmealId,setmeal.getId());
        //3.查询套餐列表
        List<SetmealDish> dishList = setmealDishMapper.selectList(sDQw);
        //4.将口味封装到套餐
        setmeal.setSetmealDishes(dishList);
        //5.返回菜品
        return setmeal;
    }
    //修改套餐内容
    @Override
    public void updateSetmeal(Setmeal setmeal) {
        //1.直接修改套餐基本信息
        //update setmeal set xx=xxx where id =1;
        setmealMapper.updateById(setmeal);
        //2.根据套餐id删除旧的菜品
        //delete from setmealdish where setmealId=1;
        LambdaQueryWrapper<SetmealDish> sDQw = new LambdaQueryWrapper<>();
        sDQw.eq(SetmealDish::getSetmealId,setmeal.getId());
        setmealDishMapper.delete(sDQw);
        //3.获取前端新提供的套餐菜品列表 遍历
        for (SetmealDish setmealDish : setmeal.getSetmealDishes()) {
            //3-1关联套餐菜品id
            setmealDish.setSetmealId(setmeal.getId());
            //3-2保存到数据库表
            setmealDishMapper.insert(setmealDish);
        }

    }
    //启停售
    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        //对集合进行非空判断
        if(CollUtil.isNotEmpty(ids)){
            //遍历ids集合
            for (Long id : ids) {
                //封装更新数据
                Setmeal setmeal = new Setmeal();
                setmeal.setId(id);
                setmeal.setStatus(status);
                //更新数据库
                setmealMapper.updateById(setmeal);
            }
        }
    }
    //套餐删除
    @Override
    public void deleteSetmeal(List<Long> ids) {
        //1.查询菜品状态
        //select count(*) from setmeal where id in(0,1) and status =1;
        LambdaQueryWrapper<Setmeal> setQw = new LambdaQueryWrapper<>();
        setQw.in(Setmeal::getId,ids);
        setQw.eq(Setmeal::getStatus,1);
        Integer setCount = setmealMapper.selectCount(setQw);
        if(setCount>0){
            throw new CustomException("有起售套餐，无法删除");
        }
        //2.批量删除套餐
        //delete from setmeal where id in();
        setmealMapper.deleteBatchIds(ids);
        //3.删除套餐菜品表中对应菜品
        //delete from setmealdish where setmealid in (1,2);
        LambdaQueryWrapper<SetmealDish> sDQw = new LambdaQueryWrapper<>();
        sDQw.in(SetmealDish::getSetmealId,ids);
        setmealDishMapper.delete(sDQw);
    }

    @Override//套餐列表
    public List<Setmeal> list(Long categoryId, Integer status) {
        //1.构建条件
        //select * from setmeal where category_id =? and status = ?
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId,categoryId).eq(Setmeal::getStatus,status);

        return setmealMapper.selectList(qw);
    }
    //套餐包含的菜品回显
    @Autowired
    private DishMapper dishMapper;
    @Override
    public List<Dish> findDishBySetmealId(Long setmealId) {
        //1.根据setmealId查询setmeal_dish表
        //1个套餐对应多个dish
        //select dish_id from setmeal_dish where setmeal_id=setmealId;
        LambdaQueryWrapper<SetmealDish> sDQw = new LambdaQueryWrapper<>();
        sDQw.eq(SetmealDish::getSetmealId,setmealId);
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(sDQw);
        //2.建立dish集合封装dish对象
        List<Dish> dishList= new ArrayList<>();
        //3.遍历套餐菜品表
        for (SetmealDish setmealDish : setmealDishList) {
            //4.根据套餐菜品表dish_id一一找到套餐中的菜品

            Long dishId = setmealDish.getDishId();
            Dish dish = dishMapper.selectById(dishId);
            //4.1为菜品设置份数，然后加入dish集合
            Integer dishCopies = setmealDish.getCopies();
            dish.setCopies(dishCopies);
            dishList.add(dish);
        }
        return dishList;
    }
}
